﻿namespace FallingWordsGame
{
    using System;
    using System.Collections.Generic;

    using FallingWordsGame.Interfaces;
                                       //  viewFiedWidth          field
    public class Renderer              //  _____________|____________
    {                                  //  |            |           |
        private char[,] buffer;        //  | words      | score:    |
        private int viewFieldWidth,    //  |  falling   |           |
                    viewFieldHeight;   //  |            |           |
        
        
        // For now - width/height are values of the whole console, but viewField is half the size
        public Renderer(int height, int width)
        {
            height -= 1;
            this.buffer = new char[height, width];
            this.viewFieldHeight = height;
            this.viewFieldWidth = width / 2;
        }

        public void ClearBuffer()
        {
            for (int row = 0; row < this.buffer.GetLength(0); row++)
            {
                for (int col = 0; col < this.buffer.GetLength(1); col++)
                {
                    this.buffer[row, col] = ' ';
                }
            }
        }

        public void FlushBuffer()
        {
            Console.Clear();
            Console.SetCursorPosition(0, 0);

            for (int row = 0; row < this.buffer.GetLength(0); row++)
            {
                for (int col = 0; col < this.buffer.GetLength(1); col++)
                {
                    this.PrintChar(this.buffer[row, col]);
                }
            }
        }
       
        public void InsertIntoField(IRenderable obj)
        {
            var image = obj.GetImage();
            
            var xCoord = obj.TopLeft.X;
            var yCoord = obj.TopLeft.Y;

            try
            {
                for (int i = 0; i < image.GetLength(0); i++)
                {
                    for(int j = 0; j < image.GetLength(1); j++)
                    {
                        this.buffer[i + xCoord, j + yCoord] = image[i, j]; 
                    }
                }
            }
            catch(IndexOutOfRangeException)
            {
                Console.Error.Write("Insertng image into renrerer buffer out of bounds. Something terrible happend");
                Environment.Exit(1);
            }
        }

        private void PrintChar(
            char letter, 
            ConsoleColor foregroundColor = ConsoleColor.Red)
        {
            if (char.IsUpper(letter))
            {
                Console.ForegroundColor = foregroundColor;
                Console.Write(char.ToLower(letter));
                Console.ForegroundColor = ConsoleColor.White;
            }
            else
            {
                if (letter == '@')
                {
                    Console.Write(' ');
                }
                else
                {
                    Console.Write(letter);
                }
            }
        }
    }
}
